package com.woniuxy.cloud.batteryassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.cloud.api.BranchesQueryApi;
import com.woniuxy.cloud.batteryassembly.exception.BatteryInfoException;
import com.woniuxy.cloud.batteryassembly.mapper.BatteryTypeMapper;
import com.woniuxy.cloud.batteryassembly.model.BatteryType;
import com.woniuxy.cloud.batteryassembly.service.IBatteryTypeService;
import com.woniuxy.cloud.batteryassembly.util.WoniuBeanUtils;
import com.woniuxy.cloud.dto.BatteryBranchesDto;
import com.woniuxy.cloud.dto.BatteryTypeDto;
import com.woniuxy.cloud.param.BatteryTypeParam;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gaorui
 * @since 2022-02-09
 */
@Service
public class BatteryTypeServiceImpl extends ServiceImpl<BatteryTypeMapper, BatteryType> implements IBatteryTypeService {

    @Resource
    private BranchesQueryApi branchesQueryApi;

    @Override
    public List<BatteryTypeDto> findBatteryType(List<Integer> ids) {
        //根据电池类型id查询电池类型信息
        List<BatteryType> batteries = list(new QueryWrapper<BatteryType>().in("battery_type_id", ids).eq("type_del", 0).eq("type_status", 0));
        System.out.println(ids.size() != batteries.size());

        if (ids.size() != batteries.size()) throw new BatteryInfoException("电池类型选择有问题，无该类电池类型，或已经被删除", 10788);
        return WoniuBeanUtils.copyList(batteries, BatteryTypeDto.class);
    }

    @Override//直接查询
    public List<BatteryTypeDto> listBatteryType() {

        List<BatteryType> batteries = list(new QueryWrapper<BatteryType>().eq("type_del", 0).eq("type_status", 0));

        return WoniuBeanUtils.copyList(batteries, BatteryTypeDto.class);
    }

    @Override
    public BatteryTypeDto addNewBatteryType(BatteryTypeParam param) {
        BatteryType batteryType = getOne(new QueryWrapper<BatteryType>()
                .eq("type_name", param.getTypeName()).eq("type_price", param.getTypePrice())
                .eq("battery_price_hours", param.getBatteryPriceHours()).eq("type_capacity", param.getTypeCapacity()));

        if (!StringUtils.isEmpty(batteryType)) throw new BatteryInfoException("该电池类型已经存在，请重新输入数据", 10789);

        BatteryType type = WoniuBeanUtils.copyObject(param, BatteryType.class);
        type.setTypeStatus(0);
        type.setTypeDel(0);
        if (!save(type)) throw new BatteryInfoException("电池类型添加失败，请重试", 10790);
        return WoniuBeanUtils.copyObject(type, BatteryTypeDto.class);
    }

    @Override
    public List<BatteryTypeDto> listBatteryTypeByLocationId(Integer locationId) {
        BatteryBranchesDto batteryBranchesDto = branchesQueryApi.checkBranches(locationId);
        if (StringUtils.isEmpty(batteryBranchesDto)) throw new BatteryInfoException("该网点不存在或者未激活", 10792);
        List<BatteryType> batteryTypes = this.baseMapper.listBatteryTypeByLocationId(locationId);
        return WoniuBeanUtils.copyList(batteryTypes, BatteryTypeDto.class);
    }
}
